﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq.Expressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using CamlexNET;
using LifeInSharePoint.Common;
using LifeInSharePoint.Common.Base;
using Microsoft.Office.Server;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.Office.Server.UserProfiles;

namespace LifeInSharePoint.Blog.Common.WebParts.LISPPostAuthor
{
    [ToolboxItemAttribute(false)]
    public class LISPPostAuthor : BaseWebPart
    {

        #region Private Properties

        private const string POSTLIST = "Pages";
        private const string CONTENTTYPE = "Life In SharePoint Blog Post";
        private string transformedXML = null;
        private bool errorFound = false;
        private string errorMessage = null;

        #endregion

        private string _XSLTPath = "/_layouts/LifeInSharePoint.Blog/xslt/AuthorBio.xslt";
        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("Life In SharePoint")]
        [WebDisplayName("XSLT Path")]
        [WebDescription("Path the the XSLT")]
        public string XSLTPath
        {
            get { return _XSLTPath; }
            set { _XSLTPath = value; }
        }


        protected override void OnInit(EventArgs e)
        {
            this.ChromeType = PartChromeType.None;
            this.ExportMode = WebPartExportMode.All;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!string.IsNullOrEmpty(XSLTPath))
            {
                processData();
            }
            else
            {
                errorFound = true;
                errorMessage = "XSLT Path is not Defined - Please edit webpart Properties";
            }

        }

        protected override void CreateChildControls()
        {
            if (!errorFound)
            {
                HtmlGenericControl container = new HtmlGenericControl("Div");
                container.InnerHtml = transformedXML;
                this.Controls.Add(container);
            }
            else
            {
                Label errorMsg = new Label();
                errorMsg.Text = errorMessage;
                this.Controls.Add(errorMsg);
            }

        }

        private void processData()
        {

            SPListItem _currentItem = SPContext.Current.ListItem;

            SPUser _Author = SPUserHelper.GetSPUser(_currentItem, "Modified By");
            
            //get current profile manager
            UserProfileManager objUserProfileManager = new UserProfileManager(SPServiceContext.GetContext(SPContext.Current.Site));
            //get current users profile
            UserProfile profile = objUserProfileManager.GetUserProfile(_Author.LoginName);
            //get user image URL
            string Bio = profile[PropertyConstants.AboutMe].Value as string;
            string Name = _Author.Name;
            string Image = profile[PropertyConstants.PictureUrl].Value as string;
            if(string.IsNullOrEmpty(Image))
            {
                Image = "/_layouts/LifeInSharePoint.Blog/images/gravatar.png";
            }
            DataTable AuthorDT = new DataTable("Author");
            AuthorDT.Columns.Add("ID");
            AuthorDT.Columns.Add("Name");
            AuthorDT.Columns.Add("Bio");
            AuthorDT.Columns.Add("ProfileImage");

            AuthorDT.Rows.Add(ID, Name, Bio, Image);

            DataSet PostsDS = new DataSet("Authors");
            PostsDS.Tables.Add(AuthorDT);
            string resultXML = PostsDS.GetXml();

            transformedXML = SPWebPartHelper.ConvertXML(resultXML, SPContext.Current.Site.Url + XSLTPath);


        }

    }
}

